package com.xnyzc.lhy.mis.mapper.system;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xnyzc.lhy.common.component.config.GlobalConfig;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.mis.entity.result.cms.SysUserResults;
import com.xnyzc.lhy.mis.entity.result.user.OaSysUserManagersRes;
import com.xnyzc.lhy.mis.entity.result.user.OaSysUserRoleRes;
import com.xnyzc.lhy.mis.entity.system.OaSysUser;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author lhy@xnyzc.com
 * @since 2019-07-24
 */
public interface OaSysUserMapper extends BaseMapper<OaSysUser> {

    /**
     * 查询用户管理列表
     *
     * @return
     */
    List sysUserList(GlobalConfig config);

    /**
     * 查询用户管理列表(分页)
     *
     * @return
     */
    List<SysUserResults> sysUserListPaging(GlobalConfig config);

    @Select("SELECT DISTINCT \n" +
            "\tuser.OFFICE_ID AS officeId,\n" +
            "\toffice.name AS name\n" +
            "\tFROM oa_sys_user AS user\n" +
            "\tLEFT JOIN oa_sys_office AS office\n" +
            "\tON user.office_id=office.office_id\n" +
            "\tWHERE user.sys_user_id in (${userIds})")
    List<Map<String, Object>> getDriverManagers(@Param("userIds") String userIds);

    @Select("SELECT DISTINCT \n" +
            "\tuser.OFFICE_ID AS officeId,\n" +
            "\toffice.name AS name\n" +
            "\tFROM oa_sys_user AS user\n" +
            "\tLEFT JOIN oa_sys_office AS office\n" +
            "\tON user.office_id=office.office_id\n" +
            "\tWHERE user.expand2=${cityId} AND user.sys_user_id in (${userIds})")
    List<Map<String, Object>> getDriverManagersByCity(@Param("cityId") String cityId, @Param("userIds") String userIds);

    /**
     * 获取城市下所有的供应商
     *
     * @return
     */
    @Select("SELECT\n" +
            "\toffice.code AS officeCode,\n" +
            "\toffice.name AS officeName\n" +
            "\tFROM oa_sys_user AS user\n" +
            "\tLEFT JOIN oa_sys_user_role AS role\n" +
            "\tON user.sys_user_id=role.user_id\n" +
            "\tLEFT JOIN oa_sys_office AS office\n" +
            "\tON user.office_id=office.office_id\n" +
            "\tWHERE role.role_id=${roleId} AND user.expand2=${cityId}")
    List<Map<String, Object>> getDriverManagersByCityId(@Param("roleId") Long roleId, @Param("cityId") String cityId);
    /**
     * 获取城市下所有的供应商
     *
     * @return
     */
//    @Select("SELECT\n" +
//            "\toffice.code AS officeCode,\n" +
//            "\toffice.name AS officeName\n" +
//            "\tFROM oa_sys_user AS user\n" +
//            "\tLEFT JOIN oa_sys_user_role AS role\n" +
//            "\tON user.sys_user_id=role.user_id\n" +
//            "\tLEFT JOIN oa_sys_office AS office\n" +
//            "\tON user.office_id=office.office_id\n" +
//            "\tWHERE role.role_id=${roleId}")
//    List<Map<String, Object>> getDriverManagersByCity(@Param("roleId") Long roleId);

    @Select("SELECT oa_sys_office.office_id " +
            "FROM oa_sys_user " +
            "LEFT JOIN oa_sys_office " +
            "ON oa_sys_user.office_id=oa_sys_office.office_id " +
            "WHERE oa_sys_user.sys_user_id=${userId}  AND oa_sys_user.deleted=0")
    /**
     * 检查该用户的权限-向供应商直接指派订单是地区公司的权限
     *
     * @param userId
     * @return
     */
    Long checkUserAuth(@Param("userId") Long userId);

    /**
     * 根据tokenUser判断角色权限
     */
    OaSysUserRoleRes getUserRoleRes(@Param("userId")Long userId);

    List<OaSysUserManagersRes> selectOfficesByConditions(IPage page, @Param("name") String name, @Param("cityId") String cityId);
}
